import logging
import socket
import threading


class CalcClient:
    def __init__(self,ip='127.0.0.1',port=9999):
        self.sock = socket.socket()
        self.addr = (ip,port)
        self.event = threading.Event()

    def start(self):
        self.sock.connect(self.addr)
        threading.Thread(target=self.recv,name='clientrecv').start()
    def stop(self):
        self.sock.close()
        self.event.set()

    def recv(self):
        while not self.event.is_set():
            try:
                data = self.sock.recv(1024)
            except Exception as e:
                logging.error(e)
                break
            if data.strip() != b'':
                print(data.strip())



    def send(self,data:str):

        self.sock.send("{}\n".format(data.strip()).encode())


def main():
    cc = CalcClient()
    cc.start()
    while True:
        cmd = input("Please input binary operation(EX 1+2) >>")
        if cmd.strip() == 'quit':
            cc.stop()
            break
        if cmd.strip() == '':
            continue
        cc.send(cmd)

if __name__ == '__main__':
    main()